package com.dayatang.hrm.compensation.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("2")
@NamedQueries( {
		@NamedQuery(name = "SalaryItem.findBySalaryItemType", query = "select new SalaryItem(o.abolishDate, suite, setting, itemType) from SalaryItemSetting setting inner join setting.salaryCalItem o inner join setting.salaryItemType itemType inner join itemType.salaryAccountSuite suite where (itemType = :pItemType and o.abolishDate >= :pQueryDate and setting.fromDate <= :pQueryDate and setting.toDate >= :pQueryDate)"),
		@NamedQuery(name = "SalaryItem.findSalaryItemsBySalaryAccountSuite", query = "select new SalaryItem(o.abolishDate, suite, setting, itemType) from SalaryItemSetting setting inner join setting.salaryCalItem o inner join o.salaryAccountSuite suite left join setting.salaryItemType itemType where (suite = :pSuite and o.abolishDate >= :pQueryDate and  setting.fromDate <= :pQueryDate and setting.toDate >= :pQueryDate)") })
public class SalaryItem extends SalaryCalculateItem {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2061585752708375040L;

	/*
	 * =======================================
	 * 
	 * transient
	 * 
	 * ========================================
	 */

	public SalaryItem() {
		setLatestSetting(SalaryItemSetting.init(this));
	}

	public SalaryItem(Date abolishDate, SalaryAccountSuite salaryAccountSuite,
			SalaryItemSetting latestSetting, SalaryItemType itemType) {
		setAbolishDate(abolishDate);
		setSalaryAccountSuite(salaryAccountSuite);
		setLatestSetting(latestSetting);
		setSalaryItemType(itemType);
	}

	// 代理属性
	public SalaryItemType getSalaryItemType() {
		return getLatestSetting().getSalaryItemType();
	}

	public void setSalaryItemType(SalaryItemType salaryItemType) {
		getLatestSetting().setSalaryItemType(salaryItemType);
	}

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	/**
	 * 找到指定日期下的薪资项目（填充薪资项目设置信息）
	 * 
	 * @param id
	 * @param queryDate
	 * @return
	 */
	public static SalaryItem get(Long id, Date queryDate) {
		SalaryItem item = getSalaryCalItemRepository()
				.get(SalaryItem.class, id);
		if (item != null) {
			SalaryItemSetting setting = SalaryItemSetting.get(item, queryDate);
			if (setting == null) {
				setting = SalaryItemSetting.init(item);
			}
			item.setLatestSetting(setting);
		}
		return item;
	}

	public static SalaryItem getLatest(Long id) {
		return get(id, new Date());
	}

	/**
	 * 根据薪资项目类型找到指定日期下，未被废除的薪资项目集合。（填充薪资项目设置信息）
	 * 
	 * @param itemType
	 * @param queryDate
	 * @return
	 */
	public static List<SalaryItem> findBySalaryItemType(
			SalaryItemType itemType, Date queryDate) {
		return getSalaryCalItemRepository().findBySalaryItemType(itemType,
				queryDate);
	}

	/**
	 * 找到指定日期下，薪资帐套下有效的薪资项目集合
	 * 
	 * @param accountSuite
	 * @param queryDate
	 * @return
	 */
	public static List<SalaryItem> findSalaryItemsBySalaryAccountSuite(
			SalaryAccountSuite accountSuite, Date queryDate) {
		return getSalaryCalItemRepository()
				.findSalaryItemsBySalaryAccountSuite(accountSuite, queryDate);
	}
}
